package operation;

import book.Book;
import book.BookList;

import java.util.Arrays;
import java.util.Scanner;

public class FindBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要查找的图书名:");
        //记录找到几本同名的图书
        int num = 0;
        //用于存放查找出的图书
        Book[] Books = new Book[bookList.books.size()];
        String name = scan.next();
        for (int i = 0; i < bookList.books.size(); i++) {
            //检查是否重名
            Book book = bookList.books.get(i);
            if (book.name.contains(name)) {
                Books[num] = book;
                num++;
            }
        }
        //(id = null会无法进行排序,所以定义新数组去除null)
        Book[] BOOKS = Arrays.copyOf(Books,num);
        Arrays.sort(BOOKS, Book::compareToid);
        if (num > 0) {
            System.out.println("找到了" + num + "本与 '" + name + "' 相关的图书:");
            for(int i = 0 ;i < num;i++){
                System.out.println(BOOKS[i]);
            }
        }else{
            System.out.println("没有找到该名的图书");
        }
    }
}
